package simpleHttpClient;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Driver class for the simple HTTP client.
 * 
 * @author Rebecca Chandler
 * Scott Wendlandt
 * Li Weihan
 *
 */

public class SimpleHTTPClient {

	@SuppressWarnings("unused")
	public static void main (String args[]){
		ConsoleUI ui = new ConsoleUI(new Scanner (System.in), new PrintWriter(System.out, true));
		ui.displayResponse("Please select a location to store downloaded files.");
		File fileLocation = ui.promptFileLocation();

		//condition determines whether the while loop should continue running
		boolean keepRunning = true;

		//variables to be reused within the while loop
		Response response;
		ClientServerConnection connection;
		Request[] requests;

		//This while loop continues prompting the user for an action and executing the selected action until the user selects exit
		while(keepRunning)
			switch(ui.promptNextAction()) {

			//executes an HTTP request and displays the response
			case HTTP_REQUEST:
				connection = new ClientServerConnection(fileLocation);
				connection.setPortNumber(ui.promptPortNumber());
				Request test = ui.promptRequest();
				ui.displayResponse(test.toString());
				response = connection.initialRequest(test);
				connection.close();

				//display response if an error did not occur
				if (response == null){
					ui.displayResponse("An error occured in processing the HTTP request.  Request may be malformed.");
				} else{
					ui.displayResponse(response.toString().trim() + "\n");
					if(!(response.getStatusLine().contains("200"))){
						ui.displayResponse("Reponses status is not 200 OK.  Entity body was not read.\n");
					} else {
						ui.displayResponse("File stored.\n" +
								"File Location: " + fileLocation.getAbsolutePath() + "\n" +
								"File Name: " + response.getFileName() + "\n");
					}
				}
				break;

				//prompts the user for a location to store the downloaded files and stores his choice
			case CHANGE_FILE_LOC:
				fileLocation = ui.promptFileLocation();
				ui.displayResponse("File Location Changed: " + fileLocation.getAbsolutePath());
				break;

				//prompts the user to select an HTML file in which to scan for downloadable elements.  Downloads those elements and displays a response.
			case DL_HTML_ELEMENTS:
				File htmlFile = ui.promptHTMLFile();

				//requests = null; //= someMethod(htmlFile); //calls a method to read URL elements from file and makes them into a collection of requests

				requests = new Utilities().loopURL(htmlFile);
				
				//creates a directory named after the HTML file in which to store the downloaded files
				File htmlDir = new File (fileLocation, htmlFile.getName());
				htmlDir.mkdir();

				//creates a ClientServerConnection which will be used for persistent connections
				//(expects requests of the same host to be grouped together and to have the persistent connect indicated in their request)
				connection = new ClientServerConnection(htmlDir);
				String hostName = "";

				ui.displayResponse("/r/n---Generating HTML element responses---");
				//iterates through the requests array
				if (requests != null){
					for(Request r: requests){
						//uses a persistent connection if the host name is the same as the previous
						if(r.getHostName().equals(hostName)){
							//						response = connection.followUpRequest(r);

							//creates a new connection if the host name is different than the previous
						} else {
							response = connection.initialRequest(r);


							hostName = r.getHostName();

							//display response if an error did not occur
							if (response == null){
								ui.displayResponse("An error occured in processing the HTTP request: " + r.getHostName() + r.getObjectPathName() +
										"\nWithin HTML document: " + htmlFile.getName());
							} else{
								ui.displayResponse(""+response);
							}
						}
					}
				}
				ui.displayResponse("---HTML elements downloaded.---/r/n" +
						"Files saved at " + fileLocation);
			case EXIT:
				keepRunning = false;
				break;

			default:
				break;

			}

		ui.close();
	}
}
